﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using System.Collections;
using System.ComponentModel.Design;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Base;

namespace ProgNetComponentsDx.Design
{
    [ToolboxItem(false)]
    internal partial class ColumnRepositoryCreator : XtraForm
    {
        IDesignerHost host = null;
        public IDesignerHost Host
        {
            get { return host; }
            set
            {
                host = value;
                DevExpress.XtraEditors.Registrator.EditorRegistrationInfo.Default.RegisterUserItems(value);
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    if (c is RepositoryData)
                    {
                        RepositoryData rep = c as RepositoryData;
                        rep.Host = host; ;
                    }
                }
            }
        }

        private ColumnRepositoryCreator()
        {
            InitializeComponent();
        }
        List<GridColumn> Columns;
        public ColumnRepositoryCreator(List<GridColumn> columns)
        {
            InitializeComponent();
            Columns = columns;
            columns.Sort(new GridColumnComaprator());
            foreach (GridColumn col in columns)
            {
                RepositoryData data = new RepositoryData(col);
                data.Host = host;
                flowLayoutPanel1.Controls.Add(data);
            }
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCreateDefaults_Click(object sender, EventArgs e)
        {
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                if (c is RepositoryData)
                {
                    RepositoryData rep = c as RepositoryData;
                    if (rep.Repository == null)
                    {
                        rep.CreateDefault();
                    }
                }
            }
        }

        private void btnAssignAll_Click(object sender, EventArgs e)
        {
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                if (c is RepositoryData)
                {
                    RepositoryData rep = c as RepositoryData;
                    rep.AssignRepository();
                }
            }
        }

        private void btnRemoveUnused_Click(object sender, EventArgs e)
        {
            if (Columns != null && Columns.Count > 0)
            {
                foreach (GridColumn c in Columns)
                {
                    if (c.View != null)
                    {
                        if (c.View.GridControl != null)
                        {
                            
                        }
                    }
                }
            }
        }

        private void btnRemoveUnused_Click_1(object sender, EventArgs e)
        {
            List<GridControl> grids = new List<GridControl>();
            foreach (GridColumn col in Columns)
            {
                if (!grids.Contains(col.View.GridControl))
                {
                    grids.Add(col.View.GridControl);
                }
            }
            foreach (GridControl gc in grids)
            {
                List<RepositoryItem> itemsToDelete = new List<RepositoryItem>();
                foreach (RepositoryItem item in gc.RepositoryItems)
                {
                    bool isAssigned = false;
                    foreach (BaseView bv in gc.ViewCollection)
                    {
                        if (bv is ColumnView)
                        {
                            foreach (GridColumn c in (bv as ColumnView).Columns)
                            {
                                if (c.ColumnEdit == item)
                                {
                                    isAssigned = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!isAssigned)
                        itemsToDelete.Add(item);
                }
                foreach (RepositoryItem i in itemsToDelete)
                {
                    gc.RepositoryItems.Remove(i);
                    host.DestroyComponent(i);
                }
            }
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                if (c is RepositoryData)
                {
                    RepositoryData rep = c as RepositoryData;
                    rep.GetAvilableRepositories();
                }
            }
        }
    }
    internal class GridColumnComaprator : IComparer<GridColumn>
    {

        #region IComparer<GridColumn> Members

        public int Compare(GridColumn x, GridColumn y)
        {
            return x.Name.CompareTo(y.Name);
        }

        #endregion
    }
}
